﻿using HCMS.CommonDtos;
using HCMS.Entities.Base;
using HCMS.IRepositories.Base;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.EntityFrameworkCore;

namespace HCMS.Repositories.Base
{
    /// <summary>
    /// 公告 接收 对象 仓储层
    /// </summary>
    public class NoticeReceiveRepository : BaseRepository<NoticeReceive>, INoticeReceiveRepository
    {
        public NoticeReceiveRepository(IDbContextProvider<BaseDbContext> dbContextProvider)
            : base(dbContextProvider)
        {
        }

        /// <summary>
        /// 管理 列表
        /// </summary>
        /// <param name="searchArgs">搜索 参数</param>
        /// <param name="pager">分页 参数</param>
        /// <returns></returns>
        public async Task<ListPagerDto<NoticeReceive>> ManageList(SearchArgsDto searchArgs, PagerArgsDto pager)
        {
            var query = await GetQueryableAsync();

            if (!string.IsNullOrWhiteSpace(searchArgs.SearchName) && !string.IsNullOrWhiteSpace(searchArgs.SearchKey))
            {
                switch (searchArgs.SearchName)
                {
                    case "userIds":
                        var ids = searchArgs.SearchKey.Split(',', StringSplitOptions.RemoveEmptyEntries);
                        if (ids != null && ids.Length > 0)
                        {
                            var idList = Array.ConvertAll(ids, int.Parse);
                            query = query.Where(t => idList.Contains(t.UserId));
                        }
                        else
                        {
                            query = query.Where(t => 1 == 2);
                        }
                        break;
                }
            }

            if (searchArgs.FromId > 0)
            {
                query = query.Where(t => t.NoticeId == searchArgs.FromId);
            }

            if (searchArgs.Status != 9999)
            {
                query = query.Where(t => t.Status == searchArgs.Status);
            }

            var count = await query.CountAsync();
            pager.RowCount = count;

            var list = new List<NoticeReceive>();

            if (count > 0)
            {
                query = query.OrderByDescending(t => t.Id);

                list = await query
                           .Skip((pager.CurrentPageIndex - 1) * pager.PageSize)
                           .Take(pager.PageSize)
                           .ToListAsync();
            }

            var result = new ListPagerDto<NoticeReceive>()
            {
                Data = list ?? new List<NoticeReceive>(),
                PagerInfo = pager
            };

            return result;
        }

        /// <summary>
        /// 根据 接收人 Id 获取列表
        /// </summary>
        /// <param name="userId">接收人 Id</param>
        /// <returns></returns>
        public async Task<List<NoticeReceive>> ListByUserId(int userId)
        {
            var query = await GetQueryableAsync();
            var result = query?.Where(t => t.UserId == userId)?.ToList();

            return result ?? new List<NoticeReceive>();
        }
    }
}
